package org.net.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class FetchUtil {

	private static Logger log = LoggerFactory.getLogger(FetchUtil.class);
	
	public static String getTargetStr(String urlStr, String charset) {
        URL url = null;
        HttpURLConnection httpConn = null;
        InputStream in = null;
        String cookie = "";
        try {
                do {
                        if (StringUtil.isEmpty(urlStr)) {
                                return "";
                        }

                        url = new URL(urlStr);
                        httpConn = (HttpURLConnection) url.openConnection();
                        HttpURLConnection.setFollowRedirects(true);
                        httpConn.setRequestMethod("GET");
                        httpConn.setConnectTimeout(10000);
                        httpConn.setRequestProperty("User-Agent",
                                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
                        if (cookie.length() != 0)
                                httpConn.setRequestProperty("Cookie", cookie);
                        httpConn.setInstanceFollowRedirects(false);
                        int code = httpConn.getResponseCode();
                        if (code == HttpURLConnection.HTTP_MOVED_TEMP) {
                                cookie += httpConn.getHeaderField("Set-Cookie") + ";";
                        }
                        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
                                break;
                } while (true);
                in = httpConn.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in,
                                StringUtil.isEmpty(charset) ? "gb2312" : charset));
                String line = "";
                StringBuffer sbf = new StringBuffer();
                while ((line = br.readLine()) != null) {
                        sbf.append(line);
                }
                return sbf.toString();
        } catch (MalformedURLException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        } finally {
                try {
                        in.close();
                        httpConn.disconnect();
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
        }
        return "";
}
	

	public static String getHTMLBufferedReader(String url) throws Exception {
		try{
		HttpClient httpClient = initClient();
		setProxy(httpClient);
		GetMethod getMethod = new GetMethod(url);
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,   
              new DefaultHttpMethodRetryHandler(1, false));
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);  
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,30000); 
		int statusCode = httpClient.executeMethod(getMethod);
		if (statusCode != HttpStatus.SC_OK) {
			System.err.println("Method failed: " + getMethod.getStatusLine());
			return null;
		}
		
		// 读取内容
		InputStream   txtis   =   getMethod.getResponseBodyAsStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(txtis));

		String tempbf;
		StringBuffer html=new StringBuffer();
		while((tempbf=br.readLine())!=null){
		html.append(tempbf);
		} 
		
//		String responseBody = getMethod.getResponseBodyAsString(); 
		String responseBody = html.toString();
		
		getMethod.releaseConnection();
		return responseBody;
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	public static InputStream getHTMLInputStream(String url) throws Exception {
		try{
		HttpClient httpClient = initClient();
		setProxy(httpClient); 
		GetMethod getMethod = new GetMethod(url);
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,   
	              new DefaultHttpMethodRetryHandler(0, false));
	    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);  
	    getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,30000); 
        log.info("Ready to crawl："+url);
		int statusCode = httpClient.executeMethod(getMethod);
		if (statusCode != HttpStatus.SC_OK) {
			System.err.println("Method failed: " + getMethod.getStatusLine());
			return null;
		}
		
		// 读取内容
		InputStream   txtis   =   getMethod.getResponseBodyAsStream();
		getMethod.releaseConnection();
		return txtis;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static void setProxy(HttpClient httpClient){
//		httpClient.getHostConfiguration().setProxy("127.0.0.1", 8087);
//		List<Header> headers = new ArrayList<Header>();   
//        headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));   
//        httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
	}
	
	 public static String getHTMLResponseBodyAsString(String url) throws Exception {  
		 try{
         HttpClient httpClient = initClient();  
         setProxy(httpClient);
         GetMethod getMethod = new GetMethod(url);
         log.info("Ready to crawl："+url);
//         System.err.println("Ready to crawl："+url);
//         getMethod.getParams().setContentCharset("gb2312");
         getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,   
                 new DefaultHttpMethodRetryHandler(0, false));
         httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);  
         getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,30000); 
         
         int statusCode = httpClient.executeMethod(getMethod);  
         if (statusCode != HttpStatus.SC_OK) {  
             System.err.println("Method failed: " + getMethod.getStatusLine());  
             return null;  
         }  
         // 读取内容  
         String responseBody = getMethod.getResponseBodyAsString();  
         getMethod.releaseConnection();  
         return responseBody;  
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 return "";
   
     }
	 
	 public static HttpClient initClient(){
		 DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(0, false);
//         HttpClientParams clientParams = new HttpClientParams();
//         clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
//         clientParams.setParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, 30000);
//         clientParams.setSoTimeout(30000);
//         clientParams.setConnectionManagerTimeout(30000);
         MultiThreadedHttpConnectionManager connectionManager =new MultiThreadedHttpConnectionManager();
         HttpConnectionManagerParams params = new HttpConnectionManagerParams();
         params.setConnectionTimeout(30000);
         params.setSoTimeout(30000);
         params.setStaleCheckingEnabled(true);
         params.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
         params.setParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, 30000);
         connectionManager.setParams(params);
         HttpClient client = new HttpClient(connectionManager);
//         HttpClient client = new HttpClient(clientParams,new SimpleHttpConnectionManager(true));
         return client;
	 }
	 
	 public static void main(String[] args) throws Exception{
		 //System.out.println(getHTMLInputStream("http://www.parentingideas.org/articles/general/commitment-teaching-children-the-lessons-of-a-lifetime"));
		 String url ="www.louboutinstore2011.com";
		 String targetUrl ="http://siteexplorer.search.yahoo.com/search?p="+url+"&fr=sfp&bwm=i";
		 String responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
		 if(!StringUtil.isEmpty(responseBody)){
			Pattern p = null;
			Matcher m = null;
			StringBuffer sb0 = new StringBuffer();
			String regex ="</li><li><span class=\"btn\">Inlinks(.*)</span></li></ul></div><div id=\"inlink-options\" class=\"opt-list opt-right\"><form method=\"get\" action=\"http://siteexplorer.search.yahoo.com/search\"><label for=\"bwmo\" class=\"optdesc\">Show Inlinks:</label>";
			p = Pattern.compile(regex);
			m = p.matcher(responseBody);
			if (m.find()) {
				sb0.append(m.group(1));
			}
			System.out.println(sb0.toString());
		 }
		 
//		 url = "lrtool.net";
		 targetUrl ="http://www.whois-search.com/whois/"+url;
		 responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
		 if(!StringUtil.isEmpty(responseBody)){
			System.out.println(responseBody);
			Pattern p = null;
			Matcher m = null;
			StringBuffer sb0 = new StringBuffer();
			String regex ="Creation Date:(.*)";
			//String regex ="<span>Created:</span></div><div class=\"form-field wide-80 normal\">(.*)</div></div><div id=\"rTab-expires\" class=\"float-row display-input\">";
			p = Pattern.compile(regex);
			m = p.matcher(responseBody);
			while (m.find()) {
				sb0.append(m.group(1));
			}
			
			System.out.println(sb0.toString());
		 }
	 }

}
